package com.jss.lanouhn.fristapp.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.modles.TodayPrice;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/3.
 */
public class HomeSmallFreeFragmentToday extends Fragment {
    private ImageView iv,iv_background;
    private TextView tv_top, tv_price, tv_time, tv_count;
    private String url = "http://api.liwushuo.com/v2/daily_lucky/today";
    private TodayPrice price;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_free_left, container, false);
        iv = (ImageView) view.findViewById(R.id.iv_free_left);
        tv_top = (TextView) view.findViewById(R.id.tv_free_top);
        tv_price = (TextView) view.findViewById(R.id.tv_free_top_price);
        tv_time = (TextView) view.findViewById(R.id.tv_free_left_time);
        tv_count = (TextView) view.findViewById(R.id.tv_free_right_count);
        iv_background= (ImageView) view.findViewById(R.id.iv_fragment_free_left);
        initData();
        return view;
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        price = gson.fromJson(result, TodayPrice.class);
                        handler.sendEmptyMessage(1);
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                tv_top.setText(price.getData().getItem().getTitle());
                tv_price.setText("￥"+price.getData().getItem().getFixed_price());
                tv_count.setText(price.getData().getVisitors()+"");
                Picasso.with(getActivity()).load(price.getData().getItem().getCover_image_url()).into(iv);
                Picasso.with(getActivity()).load(price.getData().getImage_background()).into(iv_background);
            }
        }
    };
}

